<?php
/**
 * 项目
 *
 * @author Administrator
 *
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    //
    protected $table = 'project';

    protected $guarded = [''];

    protected function getDateFormat()
    {
        return 'U';
    }

//    protected $fillable = array('subject', 'title', 'description', 'zone_id');



    const TOTAL_SCORE = 100;

    const TOTAL_ADMIN_SCORE = 100;

    protected $_progress_option = array(
        //1.项目基本信息，9%
        'photo'           => -1,//Logo
        'subject'         => -1,//项目名称
        'description'     => -1,//简介
        'zone_id'         => -1,//地区
        'is_categories'   => -1,//行业
        'is_tags'         => -1,//关键字
        'project_period'  => -2,//项目阶段（状态）
        'team_size'       => -1,//团队规模
        //项目概况30%
        'website'         => -1,//产品链接
        'is_images'       => -1,//产品图片
        'content'         => -18,//完善项目详情
        'is_developments' => -10,//发展动态
        //投资者6问6%
        'is_question'     => -12,
        //融资方案15%
        'invest_fund'     => -2,//融资金额
        'invest_way'      => -2,//融资方式 股权融资或债权融资
        'fund_plan'       => -6,//资金用途
        'business_plan'   => -2,//商业计划书
        'bdo_state'       => -3,//完善运营状况
        //团队成员40%
        'is_team'         => -12,//创始人
        'is_team_members' => -12,//团队成员
        'team_advantage'  => -10//团队优势

    );

    public function photos()
    {
        return $this->hasMany('App\Models\ProjectPhoto', 'project_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'uid', 'uid');
    }

    public function admin()
    {
        return $this->belongsTo('App\Models\Admin', 'review_admin_id', 'id');
    }

    public function zone()
    {
        return $this->belongsTo('App\Models\Zone', 'zone_id', 'id');
    }

    public function categories()
    {
        return $this->belongsToMany('App\Models\Category', 'project_category', 'project_id', 'category_id');
    }

    public function plans()
    {
        return $this->belongsToMany('App\Models\FundPlan', 'project_fund_plan', 'project_id', 'fund_plan_id');
    }

    public function answers()
    {
        return $this->hasMany('App\Models\ProjectAnswer', 'project_id', 'id');
    }

    public function activitys()
    {
        return $this->belongsToMany('App\Models\Activity', 'activity_project', 'project_id', 'activity_id');
    }

    public function members()
    {
        return $this->hasMany('App\Models\ProjectMember', 'project_id', 'id');
    }

    public function city()
    {
        return $this->belongsTo('App\Models\Zone', 'zone_id', 'id');
    }

    public function favourite()
    {
        return $this->hasMany('App\Models\Favourite', 'post_id', 'id');
    }

    public function viewer()
    {

        return $this->hasMany('App\Models\ProjectViewer', 'project_id', 'id');
    }


    /*
     * getter 获得完整度
     */
    function Progress()
    {

        $total = self::TOTAL_SCORE;
        foreach ($this->_progress_option as $key => $val) {
            if ($key == "is_team") {
                if (empty($this->leader->id)) {
                    $total += $val;
                }
            } elseif ($key == "is_question") {
                if (!count($this->answers)) {
                    $total += ($val + count($this->answers) * 2);
                }
            } elseif ($key == "is_team_members") {
                if (!count($this->members)) {
                    $total += $val;
                }
            } elseif ($key == "is_images") {
                if (!count($this->photos)) {
                    $total += $val;
                }
            } elseif ($key == "is_categories") {
                if (!count($this->categories)) {
                    $total += $val;
                }
            } elseif ($key == "is_tags") {
                if (!count($this->tags)) {
                    $total += $val;
                }
            } elseif ($key == "is_developments") {
                if (!count($this->developments)) {
                    $total += $val;
                }
            }
            if (empty($this->{$key}) && !in_array($key, array('is_images', 'is_team_members', 'is_question', 'is_team', 'is_categories', 'is_tags', 'is_developments'))) {
                $total += $val;
            }
        }

        return $total;
    }

    /**
     * 获得项目点击 播放等数量
     */
    static function getViews($project_id)
    {
        $project_views = self::where('id', $project_id)->first()->views;

        $video_views = isset(ActivityProjectVideo::where('project_id', $project_id)->first()->views) ?: 0;

        return array('project_views' => $project_views, 'video_views' => $video_views, 'share_views' => 100);
    }

    /**
     * 项目评估分数
     */
    static function views_score($project_id)
    {
        $score = 0;
        $views = self::getViews($project_id);
        $score += (int)$views['project_views'];
        $score += (int)$views['video_views'] * 4;
        $score += (int)$views['share_views'];

        return ($score / 100);
    }

    /**
     * 获得用户发布的项目数量
     */
    static function getProjectCountByUid($uid)
    {
        return self::where('uid', $uid)->count();
    }

    /**
     * 刷新项目完整度
     */
    static function refreshProgress($id)
    {
        $project = self::where('id', $id)->first();
        $progress = $project->Progress();

        return self::where('id', $id)->update(array('progress' => $progress));
    }
}
